package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        setmealMapper.changeStatus(status,id);
    }

    @Transactional
    @Override
    public void add(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.add(setmeal);

        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
//        if (setmealDishes == null || setmealDishes.size() == 0){
//            return;
//        }

        Long setmealId = setmeal.getId();
        setmealDishes.forEach(e->e.setSetmealId(setmealId));
        setmealMapper.addSetmealDish(setmealDishes);
    }

    @Override
    public SetmealVO findById(Long id) {
        SetmealVO setmealVO = setmealMapper.findById(id);

        log.info("setmealVO的结果：{}",setmealVO);

        return setmealVO;
    }


    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        if (ids == null || ids.size() ==0){
            throw new DeletionNotAllowedException("请选择删除对象");
        }

        Integer count = setmealMapper.countStatusBySetmealIds(ids);
        if (count != StatusConstant.DISABLE){
            throw new DeletionNotAllowedException("在售套餐不可删除");
        }

        setmealMapper.deleteSetmealDishBySetmealIds(ids);
        setmealMapper.deleteByIds(ids);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes == null || setmealDishes.size() == 0){
            return;
        }

        Long setmealId = setmeal.getId();
        setmealDishes.forEach(e->e.setSetmealId(setmealId));

        setmealMapper.deleteSetmealDishBySetmealIds(Arrays.asList(setmealId));
        setmealMapper.addSetmealDish(setmealDishes);
    }
}
